/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2021-01-23 16:20:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
/*
 * @Author: your name
 * @Date: 2020-09-18 11:36:06
 * @LastEditTime: 2020-09-18 15:18:21
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-RESTfulAPI\app\controller\virtualEtc.js
 */
const Controller = require('egg').Controller
let moment = require('moment');
const { v4 } = require('uuid')
class ETCController extends Controller {
    constructor(ctx) {
        super(ctx)

        this.getRoadsRule = {
            mapId: { type: 'string', required: true, allowEmpty: false }
        }
        this.getNodesRule = {
            mapId: { type: 'string', required: true, allowEmpty: false }
        }
        this.getEtcStationsRule = {
            planId: { type: 'string', required: true, allowEmpty: false }
        }
        this.addEtcStationRule = {
            planId: { type: 'string', required: true, allowEmpty: false },
            roadId: { type: 'string', required: true, allowEmpty: false },
            lon: { type: 'number', required: true, allowEmpty: false },
            lat: { type: 'number', required: true, allowEmpty: false },
            stationName1: { type: 'string', required: true, allowEmpty: false },
            stationName2: { type: 'string', required: true, allowEmpty: false },
            stationType: { type: 'int', required: true, allowEmpty: false },
        }
        this.updateEtcStationRule = {
            _id: { type: 'string', required: true, allowEmpty: false },
            lon: { type: 'number', required: true, allowEmpty: false },
            lat: { type: 'number', required: true, allowEmpty: false },
            stationName1: { type: 'string', required: true, allowEmpty: false },
            stationName2: { type: 'string', required: true, allowEmpty: false },
            stationType: { type: 'int', required: true, allowEmpty: false },
        }

        this.removeEtcStationRule = {
            id: { type: 'string', required: true, allowEmpty: false },
        }
        this.getEtcStationRule = {
            id: { type: 'string', required: true, allowEmpty: false },
        }


    }

    async getRoads() {
        const { ctx } = this
        ctx.validate(this.getRoadsRule)
        let { mapId } = ctx.request.body
        let segments = await this.ctx.model.BaseSegmentModel.find({ 'manager': mapId })
        segments = segments.map((item, index) => {
            item['geometry'] = JSON.parse(item['geometry'])
            return item
        })

        ctx.helper.success({ ctx, res: { data: segments } })
    }

    async getNodes() {
        const { ctx } = this
        ctx.validate(this.getNodesRule)

        let { mapId } = ctx.request.body
        let nodes = await this.ctx.model.BasePointModel.find({ 'manager': mapId })
        nodes = nodes.map((item, index) => {
            item['geometry'] = JSON.parse(item['geometry'])
            return item
        })
        ctx.helper.success({ ctx, res: { nodes: nodes } })
    }
    async getEtcStations() {
        const { ctx } = this
        ctx.validate(this.getEtcStationsRule)

        let { planId } = ctx.request.body
        let PlanEtcStationModel = await this.ctx.model.PlanEtcStationModel.find({ planId: planId })

        ctx.helper.success({ ctx, res: { data: PlanEtcStationModel } })
    }

    async updateEtcStation() {
        const { ctx } = this
        ctx.validate(this.updateEtcStationRule)

        let { _id, roadId, lon, lat, stationName1, stationName2, stationType } = ctx.request.body
        let PlanEtcStationModel = await this.ctx.model.PlanEtcStationModel.findOneAndUpdate(
            {
                '_id': _id
            },
            {
                roadId, lon, lat, stationName1, stationName2, stationType
            },
            { upsert: true, new: true }
        )



        ctx.helper.success({ ctx, res: { PlanEtcStationModel: PlanEtcStationModel } })
    }

    async removeEtcStation() {
        const { ctx } = this
        ctx.validate(this.removeEtcStationRule)

        let { id } = ctx.request.body
        let PlanEtcStationModel = await this.ctx.model.PlanEtcStationModel.remove(
            {
                '_id': id
            },
        )
        ctx.helper.success({ ctx, res: { PlanEtcStationModel } })
    }

    async getEtcStation() {
        const { ctx } = this
        ctx.validate(this.getEtcStationRule)

        let { id } = ctx.request.body
        let PlanEtcStationModel = await this.ctx.model.PlanEtcStationModel.find({ _id: id })

        ctx.helper.success({ ctx, res: { PlanEtcStationModel: PlanEtcStationModel } })
    }


    async addEtcStation() {
        const { ctx } = this
        ctx.validate(this.addEtcStationRule)
        let { planId, roadId, lon, lat, stationName1, stationName2, stationType } = ctx.request.body
        let plan = await this.ctx.model.PlanManagerModel.findOne({ _id: planId })
        if (!plan) {
            ctx.helper.InvaidRequest({ ctx, msg: "planId 异常" })
            return
        }

        let segments = await this.ctx.model.BaseSegmentModel.findOne({ '_id': roadId, manager: plan.mapId })
        if (!segments) {
            ctx.helper.InvaidRequest({ ctx, msg: "roadId 异常" })
            return
        }

        let etcStation = await this.ctx.model.PlanEtcStationModel.create(

            {
                planId, roadId, lon, lat,
                stationName1, stationName2, stationType,
            }

        )

        etcStation = await this.ctx.model.PlanEtcStationModel.findOne({ _id: etcStation._id })
        ctx.helper.success({ ctx, res: { data: etcStation } })
    }








}


module.exports = ETCController
